//
//  BIDPayRecordViewController.m
//  zkwy
//
//  Created by mal on 2016/11/13.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDPayRecordViewController.h"
#import "BIDPayRecordCell.h"
#import "BIDPayRecordDetailViewController.h"

//交费记录
static NSString *payRecordURL = @"client/mobile/listPayRecord.action";

@interface BIDPayRecordViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
}
@end

@implementation BIDPayRecordViewController
@synthesize houseCode;
@synthesize feeCode;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"缴费记录";
    _tableView.myDelegate = self;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], payRecordURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.feeCode=%@&searchParam.feeType=%@&searchParam.EQ_HOUSECODE=%@&page=%ld&rows=%ld", feeCode, @0, houseCode, self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark -UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    static NSString *identifier = @"identifier";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDPayRecordCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDPayRecordCell *payRecordCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    NSString *strPayType = dic[@"payType"];
    PAY_TYPE payMethod = PAY_CASH;
    if([strPayType isEqualToString:@"cash"])
    {
        payMethod = PAY_CASH;
    }
    else if([strPayType isEqualToString:@"self"])
    {
        payMethod = PAY_ATM;
    }
    else if([strPayType isEqualToString:@"pos"])
    {
        payMethod = PAY_POS;
    }
    else if([strPayType isEqualToString:@"mobile"])
    {
        payMethod = PAY_APP;
    }
    [payRecordCell prepareImgViewWithPayMethod:payMethod];
    payRecordCell.houseCodeLabel.text = dic[@"houseName"];
    payRecordCell.methodLabel.text = dic[@"payTypeName"];
    payRecordCell.dateLabel.text = dic[@"payTime"];
    CGFloat amount = [dic[@"payAmount"] floatValue];
    payRecordCell.amountLabel.text = [[NSString alloc] initWithFormat:@"￥%.2f元", amount];
    cell = payRecordCell;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    NSArray *arr = self.dataSourceArr[row][@"items"];
    BIDPayRecordDetailViewController *vc = [[BIDPayRecordDetailViewController alloc] init];
    vc.payRecordDetailArr = [[NSArray alloc] initWithArray:arr];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 59.f;
}

@end
